// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI, { Message } from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import store from './store'
import {
  getRequest,
  postRequest,
  postRequestByJSON,
  deleteRequest,
  putRequest,
  uploadFileRequest
} from './utils/api'

import {
  isNotNullORBlank
} from './utils/utils'

import './utils/filter_utils'
import 'font-awesome/css/font-awesome.min.css'

import infiniteScroll from 'vue-infinite-scroll'
Vue.use(infiniteScroll)

Vue.config.productionTip = false
Vue.use(ElementUI)

Vue.prototype.getRequest = getRequest
Vue.prototype.postRequest = postRequest
Vue.prototype.postRequestByJSON = postRequestByJSON
Vue.prototype.deleteRequest = deleteRequest
Vue.prototype.putRequest = putRequest
Vue.prototype.isNotNullORBlank = isNotNullORBlank
Vue.prototype.uploadFileRequest = uploadFileRequest

router.beforeEach((to, from, next) => {
  if (!window.sessionStorage.token) {
    if (to.meta.requireAuth) {
      Message.error('请先登陆')
      next('/')
    } else {
      next()
    }
  } else {
    if (to.name === 'Login') {
      Message.success('已经登陆')
      next('/home')
    } else {
      next()
    }
  }
  if (to.meta.requireStatus) {
    if (store.state.user.status) {
      next()
    } else {
      Message.error('没有权限')
      next(from)
    }
  }
})

var app = new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: {
    App
  }
})
